package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FloatingPopulationPriceHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <Description> <br>
 *
 * @author liaojing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2022年01月20日 <br>
 * @since R9.0<br>
 */

@Service
@AttrHandler(FloatingPopulationPriceHandler.class)
public class FloatingPopulationPriceLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
            String attrValue = attr.getValue();
            //获取场景ID
            String sceneInstId = data.getSceneInstId();

            //主控板附加商品
            //型号对应的公允价
            if(KiteStringUtils.isEqual(inst.getSpecId(),"63249555")) {
                //流动人口房屋管理产品 公允价联动赋值
                setOthProdInstPrice(attrValue, inst, sceneInstId);
            }

            //配件及其它附加商品
            //产品公允价
            if(KiteStringUtils.isEqual(inst.getSpecId(),"63249366")) {
                //流动人口房屋管理产品 公允价联动赋值
                setOthProdInstPrice(attrValue, inst, sceneInstId);
            }

    }


    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
    private void setOthProdInstPrice(String value, KiteInst inst, String sceneInstId) {

        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        // 获取子产品
        KiteProdInst subProdInst = kiteProdInstList.stream().filter(kiteProdInst -> KiteStringUtils.isEqual(kiteProdInst.getSpecId(),"63249367")).findFirst().get();
        // 没有获取到子产品，则退出
        if (KiteObjectUtils.isEmpty(subProdInst)) {
            return;
        }
        // 附加产品
        KiteProdInst addProdInst = null;
        if (KiteStringUtils.isEqual(inst.getSpecId(),"63249555")) {
            addProdInst = kiteProdInstList.stream().filter(kiteProdInst -> KiteStringUtils.isEqual(kiteProdInst.getSpecId(),"63249366")).findFirst().get();
        }
        else {
            addProdInst = kiteProdInstList.stream().filter(kiteProdInst -> KiteStringUtils.isEqual(kiteProdInst.getSpecId(),"63249555")).findFirst().get();
        }
        String OtherAttrValue = "0";
        if (!KiteObjectUtils.isEmpty(addProdInst)) {
            KiteAttr kiteAttr = addProdInst.getAttr("200004115");
            if (!KiteObjectUtils.isEmpty(kiteAttr)) {
                OtherAttrValue = KiteStringUtils.isEmpty(kiteAttr.getValue()) ? kiteAttr.getDefaultValue() : kiteAttr.getValue();
            }
        }
        BigDecimal main = new BigDecimal(value);
        BigDecimal other = new BigDecimal(OtherAttrValue);
        String result = KiteStringUtils.valueOf(main.add(other).stripTrailingZeros().toPlainString());
        operateInstAttrService.setAttrValue(subProdInst, "200004115", result, sceneInstId, null);
    }
}
